﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WaiGaoQiaoWeb.Models;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.JsonEntities;

namespace WaiGaoQiaoWeb.Controllers
{
    public class ArticleInfoSelController : Controller
    {
        private ArticleSelInfo invinfo = new ArticleSelInfo();
        private Work_ArticleUnit unit = new Work_ArticleUnit();
        protected Work_ArticleInfo waInfo = new Work_ArticleInfo();
        public ActionResult ArticleInfoSelIndex()
        {
            ViewBag.ArticleList = GetArticleList();
            return View();
        }
        /// <summary>
        ///  获取物品列表
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetArticleList()
        {
            SelectListItem sl = new SelectListItem();
            sl.Selected = true;
            sl.Text = "请选择...";
            sl.Value = "0";
            IList<Work_ArticleInfo> list = waInfo.GetAllCon_Article(" AND priorID!=0");
            IList<SelectListItem> selectList =
             list.Select(m => new SelectListItem() { Text = m.ArticleName.ToString(), Value = m.ID.ToString() }).ToList();
            selectList.Insert(0, sl);
            return selectList;
        }
        /// <summary>
        /// 获取主副调食材库存列表
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult GetArticleInfoSelList(PageVM pvm)
        {
            string query = "";
            if (string.IsNullOrEmpty(pvm.query))
            {
                query = " ";
            }
            else
            {
                string param = pvm.query.ToString();
                if (param!="0")
                query = " and ID='" + param + "'";
            }
            IList<ArticleSelInfo> invlist = invinfo.SelInventoryInfoSelInfo(pvm.rp, pvm.page, query);
            JsonFlexiGridData jfgd = new JsonFlexiGridData();
            jfgd.page = pvm.page;
            jfgd.total = invinfo.GetArticleInfoSelCount(query);
            List<string> v;
            foreach (ArticleSelInfo item in invlist)
            {
                #region
                v = new List<string>();
                v.Add(item.ID.ToString());
                v.Add(item.ArticleNO.ToString());
                v.Add(item.ArticleName);
                v.Add(!string.IsNullOrEmpty(item.ArticleUnit) ? unit.GetCon_ArticleUnitById(Convert.ToInt32(item.ArticleUnit)).Unit : "");
                v.Add(item.STOCKS_COUNT.ToString());
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item.ArticleNO.ToString(),
                    cell = v
                };
                jfgd.rows.Add(fgr);
            }
            return Json(jfgd);
        }

    }
}
